package com.chinatsp.onlineapp.ui.weather;

import java.util.LinkedList;
import java.util.List;

import tsp.cn.http.tools.LogUtil;
import tsp.cn.http.tools.T;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.bean.CityList;

public class ItemCitySelectAdapter extends BaseAdapter {

	private Context mContext;
	private View root;
	private List<CityList> list_city;
	private ImageView iv_select;
	private TextView tv_position;
	private ImageView iv_position;
	final String[] citys = { "北京", "天津" };

	private int DefaultPosition = 0;
	private LinkedList<Integer> list_selectedCity = new LinkedList<Integer>();

	public ItemCitySelectAdapter(Context context, List<CityList> cityLists) {
		mContext = context;
		list_city = cityLists;
		for (int i = 0; i < 2; i++) {
			list_selectedCity.add(i);
		}
	}

	@Override
	public int getCount() {
		return list_city.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		root = LayoutInflater.from(mContext).inflate(
				R.layout.item_gv_city_select, null);
		iv_select = (ImageView) root.findViewById(R.id.iv_select);
		tv_position = (TextView) root.findViewById(R.id.tv_position);
		iv_position = (ImageView) root.findViewById(R.id.iv_position);

		
		
		
		tv_position.setText(list_city.get(position).getName());
		
		setLocalIcon(iv_position, position);
		
		setCitySelect( iv_select, position);
		root.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				itemClickSelectCity(position);

			}
		});
		//

		return root;
	}

	/**
	 * 设置默认城市icon
	 * @param v iv_icon
	 * @param position 
	 */
	
	private void setLocalIcon(View v, int position) {
		if (position != DefaultPosition) {
			v.setVisibility(View.INVISIBLE);
		}
	}



	/**
	 * 设置选中的城市
	 * @param citys
	 * @param iv
	 * @param position
	 */
	private void setCitySelect( ImageView iv, int position) {

		if (position == DefaultPosition) {
			iv_select.setSelected(true);
			return;
		}
		if (list_selectedCity.contains(position)) {
			iv_select.setSelected(true);
		} else {
			iv_select.setSelected(false);
		}
		notifyDataSetChanged();
	}
	
	/**
	 * 条目的点击事件
	 * @param position
	 */
	private void itemClickSelectCity(int position) {
		
		if (position == DefaultPosition) {
			T.show(mContext, "定位位置，不能取消选中", 0);
		} else {
			if (list_selectedCity.contains(position)) {
				list_selectedCity.remove(new Integer(position));
				setCitySelect(iv_select, position);
				for (int i = 0; i < list_selectedCity.size(); i++) {
					System.out.println(list_selectedCity.get(i));
				}
			} else {
				if (list_selectedCity.size() < 3) {
					list_selectedCity.add(new Integer(position));
					setCitySelect( iv_select, position);
					for (int i = 0; i < list_selectedCity.size(); i++) {
						System.out.println(list_selectedCity.get(i));
					}
				} else {
					String title = mContext.getString(R.string.select_city_dialog_title);
					new AlertDialog.Builder(mContext).setTitle(title)
							.setSingleChoiceItems(citys, 1, null)
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {								
								@Override
								public void onClick(DialogInterface arg0, int which) {
									
									
								}
							})
							.setNegativeButton("取消", null).show();
				}
			}
		}
	}

}
